<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="试卷名称" prop="examPaperName">
        <el-input
          v-model="queryParams.examPaperName"
          placeholder="请输入试卷名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考试开始时间">
        <el-date-picker
          v-model="daterangeExamStartTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="考试结束时间">
        <el-date-picker
          v-model="daterangeExamEndTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="试卷状态" prop="examPaperStatus">
        <el-select v-model="queryParams.examPaperStatus" placeholder="请选择试卷状态" clearable>
          <el-option
            v-for="dict in dict.type.exam_paper_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="能否重复 0:否, 1:是" prop="examCanRepeat">
        <el-select v-model="queryParams.examCanRepeat" placeholder="请选择能否重复 0:否, 1:是" clearable>
          <el-option
            v-for="dict in dict.type.exam_can_repeat"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['examination:examPaper:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['examination:examPaper:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['examination:examPaper:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['examination:examPaper:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="examPaperList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="试卷id" align="center" prop="examPaperId" />
      <el-table-column label="试卷名称" align="center" prop="examPaperName" />
      <el-table-column label="考试开始时间" align="center" prop="examStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.examStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考试注意事项" align="center" prop="examNotes" />
      <el-table-column label="考试结束时间" align="center" prop="examEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.examEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时长(分钟)" align="center" prop="examDuration" />
      <el-table-column label="试卷状态" align="center" prop="examPaperStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.exam_paper_status" :value="scope.row.examPaperStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="能否重复 0:否, 1:是" align="center" prop="examCanRepeat">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.exam_can_repeat" :value="scope.row.examCanRepeat"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['examination:examPaper:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['examination:examPaper:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改试卷对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="试卷名称" prop="examPaperName">
          <el-input v-model="form.examPaperName" placeholder="请输入试卷名称" />
        </el-form-item>
        <el-form-item label="考试开始时间" prop="examStartTime">
          <el-date-picker clearable
            v-model="form.examStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择考试开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="考试注意事项" prop="examNotes">
          <el-input v-model="form.examNotes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="考试结束时间" prop="examEndTime">
          <el-date-picker clearable
            v-model="form.examEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择考试结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="时长(分钟)" prop="examDuration">
          <el-input v-model="form.examDuration" placeholder="请输入时长(分钟)" />
        </el-form-item>
        <el-form-item label="试卷状态" prop="examPaperStatus">
          <el-select v-model="form.examPaperStatus" placeholder="请选择试卷状态">
            <el-option
              v-for="dict in dict.type.exam_paper_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="能否重复 0:否, 1:是" prop="examCanRepeat">
          <el-select v-model="form.examCanRepeat" placeholder="请选择能否重复 0:否, 1:是">
            <el-option
              v-for="dict in dict.type.exam_can_repeat"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入是否删除" />
        </el-form-item>
        <el-form-item label="${comment}" prop="exampleOne">
          <el-input v-model="form.exampleOne" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="exampleTwo">
          <el-input v-model="form.exampleTwo" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="exampleThree">
          <el-input v-model="form.exampleThree" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="exampleFour">
          <el-input v-model="form.exampleFour" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="exampleFive">
          <el-input v-model="form.exampleFive" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExamPaper, getExamPaper, delExamPaper, addExamPaper, updateExamPaper } from "@/api/examination/examPaper"

export default {
  name: "ExamPaper",
  dicts: ['exam_paper_status', 'exam_can_repeat'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 试卷表格数据
      examPaperList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // $comment时间范围
      daterangeExamStartTime: [],
      // $comment时间范围
      daterangeExamEndTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examPaperName: null,
        examStartTime: null,
        examNotes: null,
        examEndTime: null,
        examPaperStatus: null,
        examCanRepeat: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        examPaperName: [
          { required: true, message: "试卷名称不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询试卷列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeExamStartTime && '' != this.daterangeExamStartTime) {
        this.queryParams.params["beginExamStartTime"] = this.daterangeExamStartTime[0]
        this.queryParams.params["endExamStartTime"] = this.daterangeExamStartTime[1]
      }
      if (null != this.daterangeExamEndTime && '' != this.daterangeExamEndTime) {
        this.queryParams.params["beginExamEndTime"] = this.daterangeExamEndTime[0]
        this.queryParams.params["endExamEndTime"] = this.daterangeExamEndTime[1]
      }
      listExamPaper(this.queryParams).then(response => {
        this.examPaperList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        examPaperId: null,
        examPaperName: null,
        examStartTime: null,
        examNotes: null,
        examEndTime: null,
        examDuration: null,
        examPaperStatus: null,
        examCanRepeat: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        exampleOne: null,
        exampleTwo: null,
        exampleThree: null,
        exampleFour: null,
        exampleFive: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeExamStartTime = []
      this.daterangeExamEndTime = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.examPaperId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加试卷"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const examPaperId = row.examPaperId || this.ids
      getExamPaper(examPaperId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改试卷"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.examPaperId != null) {
            updateExamPaper(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addExamPaper(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const examPaperIds = row.examPaperId || this.ids
      this.$modal.confirm('是否确认删除试卷编号为"' + examPaperIds + '"的数据项？').then(function() {
        return delExamPaper(examPaperIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('examination/examPaper/export', {
        ...this.queryParams
      }, `examPaper_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
